<?php
/**
 * Created by PhpStorm.
 * User: admin
 * Date: 2021/9/6
 * Time: 13:59
 */
namespace app\admin\controller;

use app\middleware\AdminCheck;
use think\facade\Db;

class Banner {

    protected $middleware = [
        AdminCheck::class => ['except' 	=> [''] ]
    ];



    /**
    *   @SWG\Post(
    *     path="/admin/banner/add",
    *     tags={"后台轮播图相关"},
    *     summary="添加技巧题库banner",
    *     consumes={"application/json"},
    *     produces={"application/json"},
    *     @SWG\Parameter(
    *      in="query",
    *      required=true,
    *      name="image",
    *      type="string",
    *      description="image（banner图片）"),
    *     @SWG\Response(response="200",description="ok")
    *  )
    */
    /**
     * 添加考试banner
     * @return \think\response\Json
     */
    public function add(){
        validateParam('image','image');

        $data['image']=input('image');
        $data['update_at'] = currentTime();
        $data['create_at'] = currentTime();
        Db::name('banner')->insert($data);
        return success('添加成功');
    }




    /**
    *   @SWG\Post(
    *     path="/admin/banner/getList",
    *     tags={"后台轮播图相关"},
    *     summary="技巧banner图列表",
    *     consumes={"application/json"},
    *     produces={"application/json"},
    *     @SWG\Parameter(
    *      in="query",
    *      required=true,
    *      name="pageNo,pageSize",
    *      type="string",
    *      description="pageNo(第几页),pageSize（一页多少条数据）,bannerName（名称）"),
    *     @SWG\Response(response="200",description="ok")
    *  )
    */
    /**
     * 技巧banner图列表
     * @return \think\response\Json
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function getList(){
        validateParam('pageNo,pageSize','pageNo,pageSize');
        $param=input();
        $where[]=['is_deleted','=',0];
        //获取总数
        $total=Db::name('banner')
            ->where($where)
            ->count();
        $offset=makeOffset($param['pageNo'],$param['pageSize']);

        $list=Db::name('banner')
            ->where($where)
            ->limit($offset,$param['pageSize'])
            ->select();
        return success('获取技巧banner图列表',['total'=>$total,'list'=>$list]);
    }



    /**
    *   @SWG\Post(
    *     path="/admin/banner/edit",
    *     tags={"后台考试相关"},
    *     summary="编辑技巧banner图",
    *     consumes={"application/json"},
    *     produces={"application/json"},
    *     @SWG\Parameter(
    *      in="query",
    *      required=true,
    *      name="image",
    *      type="string",
    *      description="id(轮播图的id),image（banner图片）"),
    *     @SWG\Response(response="200",description="ok")
    *  )
    */
    /**
     * 编辑技巧banner图
     * @throws \think\db\exception\DbException
     */
    public function edit(){
        validateParam('id,image','id,image');

        $data['image']=input('image');//banner图片
        $data['update_at'] = currentTime();//更新时间

        Db::name('banner')
            ->where('id',input('id'))
            ->update($data);

        return success('编辑成功');
    }




    /**
    *   @SWG\Post(
    *     path="/admin/banner/delete",
    *     tags={"后台考试相关"},
    *     summary="删除技巧banner",
    *     consumes={"application/json"},
    *     produces={"application/json"},
    *     @SWG\Parameter(
    *      in="query",
    *      required=false,
    *      name="id",
    *      type="string",
    *      description="考banner的id"),
    *     @SWG\Response(response="200",description="ok")
    *  )
    */
    /**
     * 删除技巧banner
     * @return \think\response\Json
     * @throws \think\db\exception\DbException
     */
    public function delete(){
        validateParam('ids','ids');

        Db::name('banner')
            ->where('id','in',input('ids'))
            ->update(['is_deleted'=>1]);
        return success('删除成功');
    }



}